Page({
  data: {
    userInfo: {
      username: '技师用户',
      avatar: '/assets/images/default-avatar.png',
      role: 'technician',
      roleText: '技师'
    },
    phone: '',
    realName: '',
    serviceArea: '',
    experience: '',
    isCertified: false,
    certStatus: 'pending', // verified, pending
    avatarUrl: ''
  },

  onLoad: function() {
    this.loadProfile();
  },

  loadProfile: function() {
    const app = getApp();
    
    // 获取用户基本信息
    app.request({
      url: '/accounts/profile/',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            'userInfo.username': res.data.username,
            'userInfo.avatar': res.data.avatar || '/assets/images/default-avatar.png',
            phone: res.data.phone || ''
          });
        }
      }
    });
    
    // 如果是技师，获取技师信息
    if (app.globalData.role === 'technician') {
      app.request({
        url: '/accounts/technician/profile/',
        success: (res) => {
          if (res.statusCode === 200) {
            this.setData({
              realName: res.data.real_name || '',
              serviceArea: res.data.service_area || '',
              experience: res.data.experience || '',
              isCertified: res.data.is_verified || false,
              certStatus: res.data.is_verified ? 'verified' : 'pending'
            });
          }
        }
      });
    }
  },

  onPhoneChange: function(e) {
    this.setData({
      phone: e.detail.value
    });
  },

  onRealNameChange: function(e) {
    this.setData({
      realName: e.detail.value
    });
  },

  onServiceAreaChange: function(e) {
    this.setData({
      serviceArea: e.detail.value
    });
  },

  onExperienceChange: function(e) {
    this.setData({
      experience: e.detail.value
    });
  },

  onAvatarUpload: function(e) {
    this.setData({
      'userInfo.avatar': e.detail.imageUrl,
      avatarUrl: e.detail.imageUrl
    });
  },

  saveProfile: function() {
    const app = getApp();
    
    // 更新用户基本信息
    app.request({
      url: '/accounts/profile/',
      method: 'PUT',
      data: {
        phone: this.data.phone,
        avatar: this.data.avatarUrl || this.data.userInfo.avatar
      },
      success: (res) => {
        if (res.statusCode === 200) {
          // 如果是技师，更新技师信息
          if (app.globalData.role === 'technician') {
            app.request({
              url: '/accounts/technician/profile/',
              method: 'PUT',
              data: {
                real_name: this.data.realName,
                service_area: this.data.serviceArea,
                experience: this.data.experience
              },
              success: (res) => {
                if (res.statusCode === 200) {
                  wx.showToast({
                    title: '保存成功',
                    icon: 'success'
                  });
                } else {
                  wx.showToast({
                    title: '保存失败',
                    icon: 'none'
                  });
                }
              }
            });
          } else {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            });
          }
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'none'
          });
        }
      }
    });
  },

  goToCertification: function() {
    wx.navigateTo({
      url: '/pages/technician/certification'
    });
  }
})